# -*- coding: utf-8 -*-
# @Time : 2018/12/24 10:12
# @Author : Administrator
# @File : httptestV1.py
# @Project : python_demo
# @title : httplib库 -get

# 建立会话四大核心过程：建立连接，发送请求，获取响应，关闭连接
import http.client
import re


class Http_Test():
    def get_test001(self):#agileone首页--GET
        # 建立连接
        con=http.client.HTTPConnection(host="localhost",port=8088)
        # 发送请求
        con.request(method="GET",url="/agileone/")
        # 获取响应
        res_openpage=con.getresponse().read().decode()#decode(),默认utf-8
        assert "AgileOne - Welcome to Login" in res_openpage
        if "AgileOne - Welcome to Login" in res_openpage:
            print("打开首页成功，测试通过")
        else:
            print("打开首页失败，测试不通过")
        assert re.search("Welcome to Login",res_openpage)
        # 关闭连接
        con.close()

    def get_test002(self):#agileone首页--GET
        # 建立连接
        con=http.client.HTTPConnection(host="localhost",port=8088)
        # 发送请求
        con.request(method="GET",url="/smeoa/index.php?m=login&a=index")
        # 获取响应
        print(con.getresponse().read().decode())#decode(),默认utf-8
        # 关闭连接
        con.close()

    def post_test001(self):#不带headers模拟登录请求，不能登录成功
        # 建立连接
        con = http.client.HTTPConnection(host="localhost",port=8088)
        # 发送请求
        con.request(method="POST",url="/agileone/index.php/common/login",
                    body="username=admin&password=admin&savelogin=true",
                    )
        # 获取响应

        print(con.getresponse().read().decode())
        # 关闭连接
        con.close()

    def post_test002(self):
        # 建立连接
        con = http.client.HTTPConnection(host="localhost",port=8088)
        # 发送请求
        con.request(method="POST",url="/agileone/index.php/common/login",
                    body="username=admin&password=admin&savelogin=true",
                    headers={"Content-Type": "application/x-www-form-urlencoded"})
        # 获取响应
        res_login = con.getresponse().read().decode()
        print(res_login)
        assert "successful" == res_login
        if "successful" == res_login:
            print("登录成功，测试通过")
        else:
            print("登录失败，测试不通过")

        assert re.match("successful",res_login)

        assert re.match("^[a-z]+$",res_login)

        assert re.search("success",res_login)

        pattern="^.+?$"
        # 关闭连接
        con.close()

    def post_test003(self):#不带headers模拟登录请求，不能登录成功
        # 建立连接
        con = http.client.HTTPConnection(host="localhost",port=8088)
        # 发送请求
        con.request(method="POST",url="/smeoa/index.php?m=login&a=index"
                    )
        # 获取响应
        print(con.getresponse().read().decode())
        # 关闭连接
        # con.close()

    def get_test004(self):  # agileone首页--GET
        # 建立连接
        con = http.client.HTTPConnection(host="localhost", port=8088)
        # 发送请求
        con.request(method="GET", url="/agileone/")
        # 获取响应,# decode(),默认utf-8
        # con.getresponse().read().decode()

        # res_headers=con.getresponse().getheaders() #获取响应的头信息

        res_header=con.getresponse().getheader(name="Set-Cookie")
        print(res_header)
        print(res_header.split(";"))
        print(res_header.split(";")[0])
        # 关闭连接
        con.close()

    def post_test004(self):#新增公告
        # 建立连接
        con = http.client.HTTPConnection(host="localhost", port=8088)
        # 发送请求
        con.request(method="POST", url="/agileone/index.php/common/login",
                    body="username=admin&password=admin&savelogin=true",
                    headers={"Content-Type": "application/x-www-form-urlencoded"})
        # 获取响应
        cookie=con.getresponse().getheader(name="Set-Cookie")
        print(cookie.split(";")[0])
        cookie_split=cookie.split(";")[0]  #只获取PHPSESSID，仍然可以模拟

        # 建立连接，
        con = http.client.HTTPConnection(host="localhost", port=8088)
        # 发送请求，
        con.request(method="POST", url="/agileone/index.php/notice/add",
                    body="headline=你好&content=asdasdasd&scope=1&expireddate=2019-03-24".encode("utf-8"),
                    headers={"Content-Type": "application/x-www-form-urlencoded","Cookie":cookie_split})
        # 获取响应，
        res_add=con.getresponse().read().decode()
        print(res_add)
        # 关闭连接
        assert int(res_add)>0
        # re.match(pattern, string, flags=0)
        assert re.match('^\d{3,6}$',res_add)


        #测试登录接口，模拟除登录成功以外的测试用例

        #测试新增公告接口，数据库断言编号自增成功，内容正确



if __name__ == '__main__':
    Http_Test().post_test002()


